#include <jni.h>
#include "JSON_checker.h"
#include "hello_jni_JsonChecker.h"
#include <stdio.h>
#include <string.h>


JNIEXPORT jstring JNICALL
Java_hello_jni_JsonChecker_check(JNIEnv *env, jobject obj, jstring str)
{
	const char *inText = (*env)->GetStringUTFChars(env, str, 0);
	char buf[128] = "ok";
    JSON_checker jc = new_JSON_checker(20);
	int chAt = 1, chLine = 1;
	int len = strlen(inText);
	int hasErr = 0;
	// 以 UTF8 字符编码读取字符串
	utf8_decode_init(inText, len);
	int next_char = utf8_decode_next();
	for (; next_char > 0; next_char = utf8_decode_next()) {
        if (!JSON_checker_char(jc, next_char)) {
            hasErr = 1;
            break;
        }
		++chAt;
		if (next_char == '\n') {
			chAt = 1;
			++chLine;
		}
    }
    if (hasErr || !JSON_checker_done(jc)) {
        sprintf(buf, "line %d character %d: syntax error", chLine, chAt);
    }
	(*env)->ReleaseStringUTFChars(env, str, inText);
	return (*env)->NewStringUTF(env, buf);
}
